import requests
import pymysql
from MySQLdb import OperationalError

# 配置数据库
db_config = {
    'host': 'localhost',
    'user': 'root',  # 替换为你的用户名
    'password': '',  # 替换为你的密码
    'database': 'bilibili',  # 替换为你的数据库名
    'port': 3307,
    'autocommit': True  # 自动提交事务
}

url = "https://movie.douban.com/j/chart/top_list"
params = {
    "type": "11",
    "interval_id": "100:90",
    "action": "",
    "start": "",
    "limit": "20"
}

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0"
}

# 建立数据库连接
conn = MySQLdb.connect(**db_config)
cursor = conn.cursor()
print("数据库连接成功")

movie_type_list = [('纪录片', '1'), ('短片', '23'), ('情色', '6'), ('音乐', '14'),
                   ('歌舞', '7'),
                   ('家庭', '28'), ('儿童', '8'), ('传记', '2'), ('历史', '4'), ('战争', '22'), ('犯罪', '3'),
                   ('西部', '27'),
                   ('奇幻', '16'), ('冒险', '15'), ('灾难', '12'), ('武侠', '29'), ('古装', '30'), ('运动', '18'),
                   ('黑色电影', '31')]

# 设置缓存列表
cache_data_list = []

for movie_type, movie_id in movie_type_list:

    # 根据interval_id 进行循环
    for interval_id in range(100, 0, -10):
        start = -20
        while True:
            start += 20
            params['start'] = start
            params["type"] = movie_id
            params["interval_id"] = f"{interval_id}:{interval_id - 10}"
            # 获取请求
            response = requests.get(url, params=params, headers=headers)
            items = response.json()
            if len(items) == 0:
                break
            for item in items:
                title = item['title']
                score = item['score']
                appraise_number = item['vote_count']
                actors_list = str(item['actors'])[1: -1]
                # print(params['start'], movie_type, params["interval_id"], title, score, appraise_number,
                #       actors_list)
                cache_data_list.append((title, score, appraise_number, actors_list))


        # sql命令
        sql = """
        INSERT INTO tb_dianying (title, score, appraise_number, actors_list)
        VALUES (%s, %s, %s, %s)
        """
        cursor.executemany(sql, cache_data_list)
        conn.commit()
        print(f"类型:{movie_type},百分比:{params["interval_id"]},已存入数据库{len(cache_data_list)}")
        cache_data_list.clear()

# 关闭数据库连接
cursor.close()
conn.close()
print("数据库连接已关闭")
